package com.rxyb.rabbitmq.toolmq.consumer;

import com.rabbitmq.client.Channel;
import com.rxyb.rabbitmq.toolmq.enums.QueueEnums;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.core.ApplicationFilterChain;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
import static com.rxyb.rabbitmq.toolmq.constants.EventConstant.*;

/**
 * @author YJH
 * @version 1.0
 * @description 日志记录
 * @date 2020/6/19 18:12
 */
@Component
@Slf4j
public class LogCustomer {

    private final RabbitTemplate rabbitTemplate;

    public LogCustomer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @RabbitListener(queues = MY_LOG2)
    @RabbitHandler
    public void process(Map obj, Channel channel, Message message) throws IOException {
        try {
            rabbitTemplate.convertAndSend(QueueEnums.R_LOG_RECORD1.getExchange(), QueueEnums.R_LOG_RECORD1.getRouting(), "2392797", message1 -> {
                message1.getMessageProperties().setDelay(2000);
                return message1;
            });
            System.out.println("________________________ 1 : " + obj.toString());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

    @RabbitListener(queues = MY_QUEUE_DEMO_DELAY)
    @RabbitHandler
    public void processDead(Map obj, Channel channel, Message message) throws IOException {
        try {
            System.out.println("xxxxxxxxxxxxxxxxxxx  : " + obj.toString());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

    @RabbitListener(queues = MY_LOG1)
    @RabbitHandler
    public void test(String obj, Channel channel, Message message) throws IOException {
        try {
            System.out.println("延迟  : " + obj.toString());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            /*channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);*/
        }
    }
}
